// import pinyin from 'pinyin';
import useClipboard from 'vue-clipboard3'  /* 复制 */
import { ElMessage } from 'element-plus';
/**获取时间*/
export default {
	formatDateTime(dateTimeString) { //接受参数,进行转换
		let dateObj = new Date(dateTimeString);
		let localTime = new Date(dateObj.getTime() + (dateObj.getTimezoneOffset() * 60000) + (8 * 60 * 60 * 1000));
		let year = localTime.getFullYear();
		let month = ('0' + (localTime.getMonth() + 1)).slice(-2);
		let day = ('0' + localTime.getDate()).slice(-2);
		let hours = ('0' + localTime.getHours()).slice(-2);
		let minutes = ('0' + localTime.getMinutes()).slice(-2);
		let seconds = ('0' + localTime.getSeconds()).slice(-2); // 获取秒数
		return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
	},
	onlyNumOnePoint(number_only) {
		console.log('number_only',number_only)
		//先把非数字的都替换掉，除了数字和小数点
		number_only = number_only.replace(/[^\d.]/g, "");
		//必须保证第一个为数字而不是小数点
		number_only = number_only.replace(/^\./g, "");
		//保证只有出现一个小数点而没有多个小数点
		number_only = number_only.replace(/\.{2,}/g, ".");
		//保证小数点只出现一次，而不能出现两次以上
		number_only = number_only.replace(".","$#$").replace(/\./g, "").replace("$#$", ".");
		//保证只能输入两个小数
		// number_only = number_only.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
		return number_only;
	},
    // 获取拼音首字母的方法
	// getPinyinInitials(text) {
	// 	const pinyinResult = pinyin(text, {
	// 		style: pinyin.STYLE_FIRST_LETTER,
	// 		heteronym: false,
	// 	});
	// 	return pinyinResult.map(item => item[0].toUpperCase()).join('');
	// },
	// 数组校验重复
	isExist(goodId,list,fieldName) {
		let bool = false
		for (let i = 0; i < list.length; i++) {
			if (list[i][fieldName] == goodId) {
				bool = true
			}
		}
		return bool
	},
	// 获取焦点
	inputFocus(event) {
		if(event.target.nodeName == 'INPUT'){
			event.target.select();
		}
	},
    // 按钮事件失去焦点
    buttonBlur(event) {
        let target = event.target;
        if (target.nodeName === 'SPAN') {
            target = event.target.parentNode;
        }
        target.blur();
    },

    // 防抖
    debounce(fn, delay) {
        let timer = null;
        return function () {
            let context = this;
            let args = arguments;
            if (timer) clearTimeout(timer);
            timer = setTimeout(function () {
                fn.apply(context, args);
            }, delay);
        };
    },

    copy(copy_text){
        let key = copy_text
        useClipboard().toClipboard(key)
        ElMessage.success('复制成功')
    },

    // 格式化数字，保留两位小数并添加千分符
    formatNumber(num,e) {
        const formatter = new Intl.NumberFormat('en-US', {
            minimumFractionDigits: e || 2,
            maximumFractionDigits: e || 2,
            useGrouping: true,
        });
        return formatter.format(num);
    },
}


// export {
// 	formatDateTime
// }
